#include<stdio.h>
#include<stdlib.h>

typedef struct{
	int coefficient;
	int power;
}monom;

int powerAt(monom *polynom, int size, int power)
{
	for(int i=0;i<size;i++)
		if(polynom[i].power == power) return polynom[i].coefficient;
	return 0;
}
void swapPolynom(monom *polynom1, monom *polynom2)
{
	monom temp;
	temp.coefficient = polynom1->coefficient;
	temp.power = polynom1->power;
	polynom1->coefficient = polynom2->coefficient;
	polynom1->power = polynom2->power;
	polynom2->coefficient = temp.coefficient;
	polynom2->power = temp.power;
}

monom *getPolynom(unsigned int *size)
{
	monom *ret;
	char s;
	int x;
	int i;
	ret = malloc(sizeof(monom) * 10);
	s = getchar();
	*size = 0;
	while(s!='\n')
	{
		if(s==' ')
		{
			s = getchar();
			continue;
		}
			
		x = s - '0';
		if(x==0)
		{
			s = getchar();
			while(s == ' ') s = getchar();
			s = getchar();
			continue;
		}
		if(s=='-')
		{
			s = getchar();
			x = s - '0';
			ret[*size].coefficient = -1 * x;
		}
		else
			ret[*size].coefficient = x;
		s = getchar();
		//if(s == '\n') break;
		while(s == ' ') s = getchar();
		ret[*size].power = s - '0';
		i = *size;
		while((ret[i].power > ret[i-1].power) && (i!=0))
		{
			swapPolynom(&ret[i], &ret[i-1]);
			i--;
		}
		if((i!=0) && (ret[i].power == ret[i-1].power))
		{
			ret[i-1].coefficient+=ret[i].coefficient;
			swapPolynom(&ret[i], &ret[*size]);
			(*size)--;
		}
		(*size)++;
		s = getchar();
		



	}
	//getchar();
	return ret;
	
}
void printPolyMul(monom *polynom1, unsigned int size1, monom *polynom2, unsigned int size2)
{
	int size,i,j;
	int curc;
	size = polynom1[0].power + polynom2[0].power;
	for(i=size;i>=0;i--)
	{
		curc=0;
		for(j=i;j>=0;j--)
		{
			curc += powerAt(polynom1, size, j) * powerAt(polynom2, size, i-j);

		}
		if(curc !=0)
		{
			if(i<size)
			{
				if(curc > 0) printf(" + ");
				else printf(" - ");
			}
			printf("%d", abs(curc));
			if(i==1)
				printf("x");
			else if(i>1)
				printf("x^%d", i);
		}
	}

}


void printPolySum(monom *polynom1, unsigned int size1, monom *polynom2, unsigned int size2)
{
	int i,j;
	i=0;
	j=0;
	while(i<size1 && j<size2)
	{
		if(polynom1[i].power == polynom2[j].power)
		{
			if((i > 0)||(j>0))
			{
				if(polynom1[i].coefficient + polynom2[j].coefficient > 0) printf(" + ");
				else printf(" - ");
			}
			printf("%d", abs(polynom1[i].coefficient + polynom2[j].coefficient));
			if(polynom1[i].power == 1)
				printf("x");
			else if(polynom1[i].power > 1)
				printf("x^%d", polynom1[i].power);
			i++;
			j++;
		}
		else if(polynom1[i].power > polynom2[j].power)
		{
			if((i>0)||(j>0))
			{
				if(polynom1[i].coefficient>0)
					printf(" + ");
				else printf(" - ");
			}
			printf("%d", abs(polynom1[i].coefficient));
			if(polynom1[i].power == 1)
				printf("x");
			else if(polynom1[i].power > 1)
				printf("x^%d", polynom1[i].power);
			i++;
		}
		else if(polynom1[i].power < polynom2[j].power)
		{
			if((i>0||j>0))
			{
				if(polynom2[j].coefficient>0)
					printf(" + ");
				else printf(" - ");
			}
			printf("%d", abs(polynom2[j].coefficient));
			if(polynom2[j].power == 1)
				printf("x");
			if(polynom2[j].power > 1)
				printf("x^%d", polynom2[j].power);
			j++;
		}
	}
	while(i<size1)
	{
		if(polynom1[i].coefficient>0)
			printf(" + ");
		else printf(" - ");
		printf("%d", abs(polynom1[i].coefficient));
		if(polynom1[i].power == 1)
			printf("x");
		if(polynom1[i].power > 1)
			printf("x^%d", polynom1[i].power);
		i++;
	}
	while(j<size2)
	{
		if(polynom2[j].coefficient>0)
			printf(" + ");
		else printf(" - ");
		printf("%d", abs(polynom2[j].coefficient));
		if(polynom2[j].power == 1)
			printf("x");
		if(polynom2[j].power > 1)
			printf("x^%d", polynom2[j].power);
		j++;
	}
}
int main()
{
	monom *polynom1, *polynom2;             // The input polynoms
	unsigned int polynom1Size,polynom2Size; // The size of each polynom
 
	printf("Please enter the first polynom:\n");
	polynom1 = getPolynom(&polynom1Size);   // get polynom 1
 
	printf("Please enter the second polynom:\n");
	polynom2 = getPolynom(&polynom2Size);   // get polynom 2
 
	printf("The multiplication of the polynoms is:\n"); // print the multiplication
	printPolyMul(polynom1, polynom1Size, polynom2, polynom2Size);
	printf("\n");
 
	printf("The sum of the polynoms is:\n"); // print the sum
	printPolySum(polynom1, polynom1Size, polynom2, polynom2Size);
	printf("\n");
 
	free(polynom1); // releasing all memory allocations
	free(polynom2);

	return 0;
}
